package common.core.util;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@Slf4j
public class PropertiesUtil {

    private static final String CONFIG_FILE = "config-dev.properties";
    private static Properties properties = new Properties();

    static {
        init();
    }

    public static void init(){
        try (InputStream input = PropertiesUtil.class.getClassLoader().getResourceAsStream(CONFIG_FILE)) {
            if (input != null) {
                properties.load(input);
            } else {
                log.error("Unable to load configuration file: " + CONFIG_FILE);
            }
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }

    public static int getInt(String str){
        try {
            return Integer.parseInt(properties.getProperty( str ));
        } catch (Exception e) {
            log.error(e.getMessage());
            return 0;
        }
    }

    public static long getLong(String str){
        try {
            return Long.parseLong( properties.getProperty( str ) );
        } catch (Exception e) {
            log.error(e.getMessage());
            return 0;
        }
    }

    public static double getDouble(String str){
        try {
            return Double.parseDouble(properties.getProperty( str ));

        } catch (Exception e) {
            log.error(e.getMessage());
            return 0;
        }
    }

    public static String getString(String str){
        try {
            return properties.getProperty( str );
        } catch (Exception e) {
            log.error(e.getMessage());
            return "";
        }
    }

    public static boolean getBoolean(String str){
        try {
            return Boolean.parseBoolean( properties.getProperty(str));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
